package com.kakavr.work.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.math.NumberUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.google.common.base.Objects;
import com.kakavr.base.service.BaseService;
import com.kakavr.page.Pagination;
import com.kakavr.page.constant.PaginationConstant;
import com.kakavr.util.SessionUtils;
import com.kakavr.work.dao.IWorkRecordDao;

@Service
public class WorkRecordService extends BaseService implements
		IWorkRecordService {
	@Resource
	private IWorkRecordDao workRecordDao;

	@Override
	public List<Map<String, Object>> selectWorkUserList(Map<String, Object> map) {
		if (Objects.equal(1, SessionUtils.getCurUser().get("isResident"))) {
			return workRecordDao.selectWorkAdminList(map);
		}
		return workRecordDao.selectWorkEmpList(map);
	}

	@Override
	public Pagination<Map<String, Object>> selectWorkList(
			Map<String, Object> map) {
		PageHelper.startPage(NumberUtils.toInt((String) map
				.get(PaginationConstant.PAGE_NO_NAME)), NumberUtils
				.toInt((String) map.get(PaginationConstant.PAGE_SIZE_NAME)));
		return new Pagination<Map<String, Object>>(
				workRecordDao.selectWorkList(map));
	}

	@Override
	public void addWorkRecord(Map<String, Object> map) {
		this.updateWorkRecord(map);
		workRecordDao.insertWorkRecord(map);
		workRecordDao.insertWorkUser(map);
	}

	@Override
	public void updateWorkRecord(Map<String, Object> map) {
		Map<String, Object> work = workRecordDao.selectStoreMaxWork(map);
		if (null == work || work.isEmpty()) {
			return;
		}
		map.putAll(work);
		Map<String, Object> shop = SessionUtils.getCurUser();
		map.put("storeId", shop.get("id"));
		Map<String, Object> rechare = workRecordDao.staticUserRecharge(map);
		map.put("totalAmount", 0);
		map.put("amount", 0);
		if (null != rechare && !rechare.isEmpty()) {
			BigDecimal totalAmount = null, three = new BigDecimal("300"), ratio = new BigDecimal(
					"0.1");
			// 如果是健身房
			if (Objects.equal(3, shop.get("typeId"))) {
				totalAmount = (BigDecimal) rechare.get("gymAmount");
			} else {
				totalAmount = (BigDecimal) rechare.get("amount");
			}
			map.put("totalAmount", totalAmount);
			if (totalAmount.compareTo(three) >= 0) {
				int userCount = workRecordDao.staticUserCount(map);
				if (userCount > 0) {
					map.put("amount",
							totalAmount.multiply(ratio).divide(
									new BigDecimal(userCount), 2,
									BigDecimal.ROUND_HALF_UP));
				}
			}
		}
		workRecordDao.updateWorkRecord(map);
		workRecordDao.updateWorkUser(map);
	}

	@Override
	public List<Map<String, Object>> staticWorkUser(Map<String, Object> map) {
		return workRecordDao.staticWorkUser(map);
	}

	@Override
	public Pagination<Map<String, Object>> staticWorkByOwn(
			Map<String, Object> map) {
		PageHelper.startPage(NumberUtils.toInt((String) map
				.get(PaginationConstant.PAGE_NO_NAME)), NumberUtils
				.toInt((String) map.get(PaginationConstant.PAGE_SIZE_NAME)));
		return new Pagination<Map<String, Object>>(
				workRecordDao.staticWorkByOwn(map));
	}

	@Override
	public Map<String, Object> selectStoreMaxWork(Map<String, Object> map) {
		map.put("time", DateTime.now().toDate());
		return workRecordDao.selectStoreMaxWork(map);
	}

}
